package com.platform.api.bzproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.platform.api.bzproject.dao.BzQuotationsMapper;
import com.platform.api.bzproject.model.BzBaseInfo;
import com.platform.api.bzproject.model.BzConfig;
import com.platform.api.bzproject.model.BzQuotations;
import com.platform.api.bzproject.service.BzBaseInfoService;
import com.platform.api.bzproject.service.BzConfigService;
import com.platform.api.common.controller.BaseController;
import com.platform.common.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author guoyunfei on 2021/7/9 14:03
 * @className BzConfigMapper
 * @description TODO
 * @Version 1.0
 * 源码联系微信：2549789059
 */
@Api(tags = "API全局配置信息")
@RestController
@RequestMapping("{appid}/config")
public class BzConfigController extends BaseController {


    @Autowired
    private BzConfigService bzConfigService;

    @Autowired
    private BzBaseInfoService bzBaseInfoService;

    @Autowired
    private BzQuotationsMapper quotationsDao;


    @ApiOperation(value = "小程序设置信息", notes="小程序设置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/setting")
    public Result setting(@PathVariable String appid){
        BzBaseInfo info = bzBaseInfoService.selectOne(appid);
        Map<String,Object> map = new HashMap(6);
        if(null != info){
            if( null != info.getAppBg() && !"".equals(info.getAppBg())){
                map.put("background",info.getAppBg());
            }else{
                List<BzConfig> user_bg = bzConfigService.getConfig("user_bg");
                map.put("background",user_bg.get(0).getValue());
            }
        }else{
            List<BzConfig> user_bg = bzConfigService.getConfig("user_bg");
            map.put("background",user_bg.get(0).getValue());
        }

        map.put("menu","");
        return Result.success(map);
    }

    @ApiOperation(value = "小程序分享信息", notes="小程序分享信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/setting_share")
    public Result setting_share(@PathVariable String appid){
        Map<String,Object> map = new HashMap(6);

        BzBaseInfo info = bzBaseInfoService.selectOne(appid);
        if(null != info){
            map.put("share_image",info.getShareImg());
            map.put("share_title",info.getShareTitle());
        }
        return Result.success(map);
    }

    @ApiOperation(value = "获取小程序分享信息", notes="获取小程序分享信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/setting_share_quotation")
    public Result setting_share_quotation(@PathVariable String appid){
        Map<String,Object> map = new HashMap(6);
        BzBaseInfo info = bzBaseInfoService.selectOne(appid);
        if(null != info){
            boolean isRandom = info.getRandomQuo() == 1 ? true : false;
            map.put("isRandom",isRandom);
            String appName = null != info.getAppName() && !"".equals(info.getAppName()) ? "【"+info.getAppName()+"】" : "";
            if(isRandom){
                //获取语录列表

                QueryWrapper<BzQuotations> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("appid",appid);
                List<BzQuotations> list = quotationsDao.selectList(queryWrapper);
                Random ra =new Random();
                int rand = ra.nextInt(list.size()) + 1;

                queryWrapper.eq("inde",rand);
                BzQuotations quotation = quotationsDao.selectOne(queryWrapper);
                map.put("share_image",quotation.getImg());
                map.put("share_title",appName + quotation.getQutotation());
            }else {
                map.put("share_image",info.getShareImg());
                map.put("share_title",appName + info.getShareTitle());
            }
            map.put("title",info.getAppName());
        }
        return Result.success(map);
    }


    @GetMapping("/getContact")
    public Result getContact(){

        Map<String,Object> resultMap = new HashMap<>();

        List<BzConfig> qq = bzConfigService.getConfig("qq");
        List<BzConfig> wx = bzConfigService.getConfig("wx");
        List<BzConfig> dy = bzConfigService.getConfig("dy");

        BzBaseInfo baseInfo = bzBaseInfoService.getOne(new QueryWrapper<BzBaseInfo>().eq("platform_type", getSystemType()));

        resultMap.put("qq",qq.size() > 0 ? qq.get(0).getValue() : null);
        resultMap.put("wx",wx.size() > 0 ? wx.get(0).getValue() : null);
        resultMap.put("dy",dy.size() > 0 ? dy.get(0).getValue() : null);
        resultMap.put("appletName",null != baseInfo ? (null != baseInfo.getAppName() ? baseInfo.getAppName() : null) : "");

        return Result.success(resultMap);
    }


    /**
     * 获取创作者菜单信息
     * @return
     */
    @GetMapping("/loadCreateConfig")
    public Result loadCreateConfig(){

        Map<String,Object> resultMap = new HashMap<>();

        List<BzConfig> close_creator = bzConfigService.getConfig("close_creator");
        List<BzConfig> close_apply = bzConfigService.getConfig("close_apply");
        List<BzConfig> close_all = bzConfigService.getConfig("close_all");

        resultMap.put("close_creator",close_creator.size() > 0 ? close_creator.get(0).getValue() : null);
        resultMap.put("close_apply",close_apply.size() > 0 ? close_apply.get(0).getValue() : null);
        resultMap.put("close_all",close_all.size() > 0 ? close_all.get(0).getValue() : null);
        return Result.success(resultMap);
    }

    /**
     * 获取配置信息
     * @param type
     * @return
     */
    @GetMapping("/getConfigs")
    public Result getConfigs(String type){

        List<BzConfig> configs = bzConfigService.getConfig(type);
        return Result.success(configs);
    }

}
